package com.tj.test.sys.service.impl;

import com.tj.test.sys.dao.AnswerMapper;
import com.tj.test.sys.po.Answer;
import com.tj.test.sys.service.IAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class AnswerService implements IAnswerService {
    @Autowired
    private AnswerMapper mapper;

    @Override
    //查询所有答卷表信息
    public List<Answer> selectAllAnswer(Answer answer) {
        List<Answer> list=new ArrayList<>();
        if (!mapper.selectAllAnswer(answer).isEmpty()){
            list=mapper.selectAllAnswer(answer);
        }
        return list;
    }

    @Override
    //插入答卷信息
    public int insertAnswer(Answer answer) {
        int temp=0;
        /* 业务判断
         * （1）名称不能重复
         * （2）增加之前先查询*/
    /*    try{*/
            temp=mapper.selectAnswerById(answer);
            if (temp<1){
                temp=mapper.insertAnswer(answer);
            }else{
                return temp;
            }
      /*  }catch (Exception e){
            return -2;
        }*/
        return temp;
    }

    @Override
    //删除答卷表信息
    public int deleteAnswer( Answer answer) {
        int temp=0;
        try{
            temp=mapper.selectAnswerById(answer);
            if (temp>0){
                temp=mapper.deleteAnswer(answer);
            }else{
                return -2;
            }
        }catch (Exception e){
            return -1;
        }
        return temp;
    }

    @Override
    //修改答卷表信息
    public int updateAnswer(Answer answer) {
        int temp=0;
        try{
            temp=mapper.selectAnswerById(answer);
            if (temp>0){
                temp=mapper.updateAnswer(answer);
            }else{
                return temp;
            }
        }catch (Exception e){
            System.out.println(e.getStackTrace());
            return -1;
        }
        return temp;
    }
}
